home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / TimGA 1.2.1 / .cp / MyUtils.cp < prev   
Encoding:
Text File  |  1997-07-16  |  5.4 KB  |  200 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    MyUtils.cp            ©1995-97 Timo Eloranta        All rights reserved.
  3. // ===========================================================================
  4. //    MyUtils.h            (press Command-Tab to open the associated header)
  5.  
  6. #include "MyUtils.h"
  7. #include <LMenuBar.h>
  8. #include <Sound.h>                // Universal header - SndPlay() etc.
  9. #include "GraphGA_PaneIDs.h"
  10.  
  11. // ---------------------------------------------------------------------------
  12. //        • FastRandom
  13. //
  14. //          Called by:    main
  15. //                        CGraphDrawing::EdgeMutation
  16. //                        CGraphDrawing::ThreeConnectedNodes
  17. //                        CPopulation::DoMutate
  18. //                        CPopulation::DoCrossover
  19. //                        RangedRdm, RandomBool
  20. // ---------------------------------------------------------------------------
  21. //    From: Andrew Welch <andrew@AmbrosiaSW.com>
  22. //    Newsgroups: comp.sys.mac.programmer.help,alt.sources.mac
  23. //    Subject: FastRandom() source
  24. //    Date: Tue, 13 Feb 1996 18:54:00 -0500
  25. //    Organization: Ambrosia Software, Inc.
  26. //
  27. //    While converting an application to be PowerPC native, I noticed that an  
  28. //    A-Trap it made heavy use of (_Random) was not yet native.  So I did a quick  
  29. //    and dirty translation of the _Random 68K assembler code into C.
  30. //
  31. //    It's pretty self-explanitory -- call FastRandom(0) to initialize the random 
  32. //    number seed, or pass any positive value in the range parameter to generate 
  33. //    a psuedo-random number between 0..range - 1
  34. //
  35. //    Enjoy... and remember, you get what you pay for. :)
  36.  
  37. short
  38. FastRandom( short range )
  39. {
  40.     short        result;
  41.     static        unsigned    long                randomSeed;
  42.     register    unsigned    long                calc;
  43.     register    unsigned    long                regD0;
  44.     register    unsigned    long                regD1;
  45.     register    unsigned    long                regD2;
  46.     
  47.     if (range == 0)
  48.         randomSeed = LMGetTicks() + LMGetTime();
  49.     else
  50.         {
  51.         calc = randomSeed;
  52.         regD0 = 0x41A7;
  53.         regD2 = regD0;
  54.         
  55.         regD0 *= calc & 0x0000FFFF;
  56.         regD1 = regD0;
  57.         
  58.         regD1 = regD1 >> 16;
  59.         
  60.         regD2 *= calc >> 16;
  61.         regD2 += regD1;
  62.         regD1 = regD2;
  63.         regD1 += regD1;
  64.         
  65.         regD1 = regD1 >> 16;
  66.         
  67.         regD0 &= 0x0000FFFF;
  68.         regD0 -= 0x7FFFFFFF;
  69.         
  70.         regD2 &= 0x00007FFF;
  71.         regD2 = (regD2 << 16) + (regD2 >> 16);
  72.         
  73.         regD2 += regD1;
  74.         regD0 += regD2;
  75.         
  76.         if (regD0 < 0)
  77.             regD0 += 0x7FFFFFFF;
  78.         
  79.         randomSeed = regD0;
  80.         
  81.         if ((regD0 & 0x0000FFFF) == 0x8000)
  82.             regD0 &= 0xFFFF0000;
  83.     
  84.     // -- Now that we have our pseudo random number, pin it to the range we want
  85.     
  86.         regD1 = range;
  87.         regD1 *= (regD0 & 0x0000FFFF);
  88.         regD1 = (regD1 >> 16);
  89.         
  90.         result = regD1;
  91.         }
  92.         
  93.     return result;
  94. }
  95.  
  96. // ---------------------------------------------------------------------------
  97. //        • PlayOneSnd
  98. //
  99. //          Called by:    CGraphGADoc::SpendTime
  100. // ---------------------------------------------------------------------------
  101. //    Play a single sound from a snd resource. If the Boolean inAsync 
  102. //    parameter is true, the sound is played asynchronously.
  103.  
  104. pascal OSErr
  105. PlayOneSnd ( ResIDT inSoundID, Boolean inAsync)
  106. {
  107.             OSErr           theOE = noErr;
  108.  
  109.     static  SndListHandle   sPlayingSound;
  110.     static  SndChannelPtr   sCurChannel;
  111.  
  112.     if ( sCurChannel ) {
  113.         if (!( theOE = ::SndDisposeChannel( sCurChannel, true )))
  114.             sCurChannel = nil;
  115.     }
  116.  
  117.     if ( sPlayingSound ) {
  118.         OSErr theOE2;
  119.         ::ReleaseResource( (Handle) sPlayingSound );
  120.         if ( !( theOE2 = ::ResError() ))
  121.             sPlayingSound = nil;
  122.         else if ( !theOE )
  123.             theOE = theOE2;
  124.     }
  125.  
  126.     if ( !theOE ) {
  127.         sPlayingSound = (SndListHandle) ::GetResource('snd ', inSoundID );
  128.         
  129.         if ( !( theOE = ::ResError() ))
  130.             if ( !sPlayingSound ) 
  131.                 theOE = resNotFound;
  132.             else {
  133.                 ::HLockHi( (Handle) sPlayingSound );
  134.             
  135.                 if ( !inAsync ) {
  136.                     if (!(theOE = ::SndPlay( nil, sPlayingSound, false ))) {
  137.                         ::ReleaseResource( (Handle) sPlayingSound );
  138.                         if ( !theOE ) 
  139.                             theOE = ::ResError ( );
  140.                         if ( !theOE ) 
  141.                             sPlayingSound = nil;
  142.                     }
  143.                 }
  144.                 else if (!(theOE = ::SndNewChannel( &sCurChannel,
  145.                                         sampledSynth, initMono, nil)))
  146.                                         
  147.                     theOE = ::SndPlay( sCurChannel, sPlayingSound, true );
  148.             }
  149.     }
  150.     return theOE;
  151. }
  152.  
  153. // ---------------------------------------------------------------------------
  154. //        • ToggleMenuItem
  155. //
  156. //          Called by:    CGraphGADoc::PrepareToIterate
  157. //                        CGraphGADoc::ObeyCommand
  158. //                        CGraphPane::CGraphPane
  159. //                        CGraphPane::ObeyCommand
  160. // ---------------------------------------------------------------------------
  161. //    An utility routine to toggle texts of menu items.
  162.  
  163. void
  164. ToggleMenuItem(
  165.     CommandT    inCommand,
  166.     ResIDT        inStringID )
  167. {
  168.     ResIDT        menuID;
  169.     MenuHandle    menuH;
  170.     Int16        menuItem;
  171.     LMenuBar    *theMenuBar = LMenuBar::GetCurrentMenuBar();
  172.     Str255        theString;
  173.     
  174.     theMenuBar -> FindMenuItem( inCommand, menuID, menuH, menuItem );
  175.     
  176.     if (menuItem != 0) {
  177.         ::GetIndString( theString, STRx_Menus, inStringID );
  178.         ::SetMenuItemText( menuH, menuItem, theString );
  179.     }
  180. }
  181.  
  182. // ---------------------------------------------------------------------------
  183. //        • MyPtInRect
  184. //
  185. //          Called by:    CGraphNodes::LargeContMutate
  186. //                        CGraphNodes::RectCrossover
  187. // ---------------------------------------------------------------------------
  188.  
  189. Boolean 
  190. MyPtInRect( Int16 inX, Int16 inY, const Rect *inRect )
  191. {
  192.     Point    thePt;
  193.     Rect    theRect = *inRect;
  194.     
  195.     SetPt( &thePt, inX, inY);
  196.     ++theRect.right; ++theRect.bottom;
  197.     
  198.     return ::PtInRect( thePt, &theRect);
  199. }
  200.